package com.byit.jgpush;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author tjm
 * @date 2018年12月24日
 */
@Service
@Slf4j
public class JgPushService {

    private static final String appKey = "ffdee8935f34ffb5700b4c7b";
    private static final String masterSecret = "25917800ea567e54d4211341";
    private static JPushClient jPushClient;

    static {
        jPushClient =  new JPushClient(masterSecret, appKey);
    }


    /**
     * 推送所有用户自定义消息
     * 所有平台
     *
     * @param param title content type
     */
    public void jpushAndroidIOSByAllForMessage(JSONObject param) {
        PushPayload payload = PushPayload.newBuilder()
                //所有平台的用户
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setMessage(Message.newBuilder()
                        .setMsgContent(param.getString("content"))
                        .setTitle(param.getString("title"))
                        .addExtra("data", param.getJSONObject("data").toJSONString())
                        .build())
                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境，false表示开发，true表示生产；对android和自定义消息无意义
                        .setApnsProduction(false)
                        //此字段是给开发者自己给推送编号，方便推送者分辨推送记录
                        .setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长，如果不传此字段则默认保存一天，最多指定保留十天，单位为秒
                        .setTimeToLive(86400)
                        .build())
                .build();
        send(payload);
    }

    /**
     * 推送指定别名用户自定义消息
     * 所有平台
     *
     * @param param title content type alias
     */
    public void jpushAndroidIOSByaliasForMessage(JSONObject param) {
        PushPayload payload = PushPayload.newBuilder()
                //所有平台的用户
                .setPlatform(Platform.android_ios())
                //用户别名
                .setAudience(Audience.alias(param.getString("alias")))
                .setMessage(Message.newBuilder()
                        .setMsgContent(param.getString("content"))
                        .setTitle(param.getString("title"))
                        .addExtra("data", param.getJSONObject("data").toJSONString())
                        .build())
                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境，false表示开发，true表示生产；对android和自定义消息无意义
                        .setApnsProduction(false)
                        //此字段是给开发者自己给推送编号，方便推送者分辨推送记录
                        .setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长，如果不传此字段则默认保存一天，最多指定保留十天，单位为秒
                        .setTimeToLive(86400)
                        .build())
                .build();
        send(payload);
    }

    /**
     * 通知
     * 所有平台 平台是 Andorid 与 iOS
     * 推送对象：所有用户
     * param: title msg type
     *
     * @return
     */
    public void jpushAllByAndroidIOS(JSONObject param) {
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        //通知内容
                        .setAlert(param.getString("msg"))
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                //通知标题
                                .setTitle(param.getString("title"))
                                //扩展字段
                                .addExtra("data", param.getJSONObject("data").toJSONString())
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("data", param.getJSONObject("data").toJSONString())
                                .build())
                        .build()
                )
                .build();
        send(payload);
    }

    /**
     * 推送指定tag标签的用户
     * 所有平台
     *
     * @param param tag titile msg type
     */
    public void jpushAndroidIOSByTag(JSONObject param) {
        PushPayload payload = PushPayload.newBuilder()
                //所有平台的用户
                .setPlatform(Platform.android_ios())
                //标签为tag1，tag2的用户
                .setAudience(Audience.tag(param.getString("tag")))
                .setNotification(Notification.newBuilder()
                        .setAlert(param.getString("msg"))
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(param.getString("title"))
                                .addExtra("data", param.getJSONObject("data").toJSONString())
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("data", param.getJSONObject("data").toJSONString())
                                .build())
                        .build()
                )
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                .build();
        send(payload);

    }

    /**
     * 通知
     * 推送指定别名的用户
     * 所有平台
     *
     * @param param title msg type alias
     */
    public void jpushAndroidIOSByAlias(JSONObject param) {
        PushPayload payload = PushPayload.newBuilder()
                //所有平台的用户
                .setPlatform(Platform.android_ios())
                //标签为tag1，tag2的用户
                .setAudience(Audience.alias(param.getString("alias")))
                .setNotification(Notification.newBuilder()
                        .setAlert(param.get("msg"))
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(param.getString("title"))
                                .addExtra("data", param.getJSONObject("data").toJSONString())
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("data", param.getJSONObject("data").toJSONString())
                                .build())
                        .build())
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //这里是指定开发环境,不用设置也没关系
                .build();
        send(payload);
    }

    /**
     * ios推送
     *
     * @param param
     * @return
     */
    public void jpushIOS(JSONObject param) {
        PushPayload payload = PushPayload.newBuilder()
                //ios平台的用户
                .setPlatform(Platform.ios())
                //所有用户
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(param.getString("msg"))
                                .setBadge(+1)
                                //这里是设置提示音(更多可以去官网看看)
                                .setSound("happy")
                                .addExtra("data", param.getJSONObject("data").toJSONString())
                                .build())
                        .build())
                //自定义信息
                .setMessage(Message.newBuilder()
                        .setMsgContent(param.getString("msg"))
                        .addExtra("data", param.getJSONObject("data").toJSONString())
                        .build())
                //此字段的值是用来指定本推送要推送的apns环境，false表示开发，true表示生产；对android和自定义消息无意
                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                .build();
        send(payload);

    }

    private void send(PushPayload payload){
        try {
            PushResult push = jPushClient.sendPush(payload);
            log.info(push.toString());
        }catch (Exception e){
            log.error("",e);
        }
    }
}